package cn.tedu.ivos2.user.service.impl;

import cn.tedu.ivos2.base.exception.ServiceException;
import cn.tedu.ivos2.base.response.StatusCode;
import cn.tedu.ivos2.user.mapper.UserMapper;
import cn.tedu.ivos2.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos2.user.pojo.dto.UserQuery;
import cn.tedu.ivos2.user.pojo.vo.UserVO;
import cn.tedu.ivos2.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service  //表示当前类为此接口对应的实现类，注入该类对象
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam){
        log.debug("用户登录业务:{}",userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        /* 如果业务层遇到了问题，不能直接返回结果给前端，而是要抛出业务层异常，让全局异常处理器来处理
        * 一旦抛出异常，结果是由全局异常处理器来返回给前端的，就不走controller了 */
        if(userVO==null){ //用户不存在
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){ //密码错误
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        log.debug("当前登录用户数据:{}",userVO);
        return userVO;
    }
    @Override
    public List<UserVO> selectUser(UserQuery userQuery){
        log.debug("查询用户业务:{}",userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        return list;
    }
}
